

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

import com.hogofwar.DJbot.BasePlugin;
import com.hogofwar.DJbot.IRCBase;

public class COM_haspaid implements BasePlugin {

	public void start(final IRCBase irc, final String[] extra, String channel,
			String sender, String hostname, boolean usename) {
		// System.out.println(extra[0]);
		// System.out.println(extra[1]);

		String start = "";
		if (usename == true) {
			start = sender + ": ";
		}

		if (extra.length != 0) {
			if (extra[0] != " ") {
				String tosend = null;
				try {
					// Create a URL for the desired page
					URL url = new URL(
							"http://www.minecraft.net/haspaid.jsp?user="
									+ extra[0]);

					BufferedReader in = new BufferedReader(
							new InputStreamReader(url.openStream()));
					String str;
					while ((str = in.readLine()) != null) {
						if (str.toLowerCase().contains("true")) {
							tosend = extra[0] + " seems to have paid.";
						} else {
							tosend = extra[0] + " seems to have not paid.";
						}
					}
					in.close();

				} catch (MalformedURLException e) {
					tosend = "Malformed URL!";
				} catch (IOException e) {
					tosend = "IOException!";
				}

				// address.getCanonicalHostName()
				// tosend = " seems to be up.";

				
				irc.sendMessage(channel, start + "" + tosend);
			} else {
				
				irc.sendMessage(channel, start + "You need to specify a user!");
			}
		} else {
			
			irc.sendMessage(channel, start + "You need to specify a user!");
		}
	}

	public String help() {
		return "Haspaid - haspaid [username] - Check if the specified user has paid for Minecraft.";
		// TODO Auto-generated method stub

	}

	// public static boolean exists(String URLName){
	// try {
	// HttpURLConnection.setFollowRedirects(true);
	// // note : you may also need
	// //HttpURLConnection.setInstanceFollowRedirects(false)
	// HttpURLConnection con =
	// (HttpURLConnection) new URL(URLName).openConnection();
	// con.setRequestMethod("HEAD");
	// return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
	// }
	// catch (Exception e) {
	// e.printStackTrace();
	// return false;
	// }
	// }
	public String version() {
		return "Haspaid version 1";
		// TODO Auto-generated method stub

	}

	@Override
	public String restrict() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean hide() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String aliases() {
		// TODO Auto-generated method stub
		return "hp, paid, mcpaid, checkpaid, userpaid";
	}

}
